#
# viewer.py
#

"""
export html code from theme, language, template
"""

import sys
from string import Template

class viewer:
	
	def __loadTheme(self, theme,language):
		
#------------------------------------------------------- load Language
		languageLocation = [sys.path[0]+'/language']
		save = sys.path
		sys.path = languageLocation
		
		try:
			languagekind = __import__(language)
		except:
			print "%s language is not exist" % language

		sys.path = save
		languageObj = getattr(languagekind,language)
		self.body['logo'] = languageObj['logo']
		
#-------------------------------------------------------- load theme
		themeLocation = [sys.path[0]+'/theme']
		save = sys.path
		sys.path = themeLocation
		
		try:
			load = __import__(theme)
		except:
			print "%s theme is not exist" % theme

		sys.path = save
		return load.html
			
		
	def __init__(self, theme, language):
		self.header	=[]
		self.body	={}
		tpl = self.__loadTheme(theme,language)
		self.template = Template(tpl)
		
		pass
	
	def addHeader(self, name, code):
		""" add Element such as 'title' 'Home', 'About, 'Help', Submit,
		'Preview'
		"""
		self.header.append( (name, code))
		pass
		
	def addBody(self, name, code):
		""" add Element such as 'title' 'Home', 'About, 'Help', Submit,
		'Preview'
		"""
		self.body[name]=code
		pass

	def getHeader(self):
		return self.header
		
	def getBody(self):
		return self.template.safe_substitute(self.body)
